package com.jctrip.hotel.source.xxl.meituan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.constant.XxlJobNameConstant;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.domain.dao.HtSourceInfoDao;
import com.jctrip.hotel.domain.entity.HtSourceInfo;
import com.jctrip.hotel.source.model.CommonJobParam;
import com.jctrip.hotel.source.service.meituan.MeituanTaskService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 美团供应静态信息拉取
 */
@Component
@Slf4j
public class SyncMeituanHotelStaticInfoPopXxlJob extends IJobHandler {
    @Resource
    private MeituanTaskService meituanTaskService;
    @Resource
    private HtSourceInfoDao htSourceInfoDao;
    @Override
    @XxlJob(XxlJobNameConstant.SYNC_MTHDOM_HOTEL_STATIC_INFO_POP)
    public void execute() throws Exception {
        XxlJobHelper.log(XxlJobNameConstant.SYNC_MTHDOM_HOTEL_STATIC_INFO_POP + " xxl job start");

        try {

            CommonJobParam commonJobParam = JsonUtil.toObject(CommonJobParam.class, XxlJobHelper.getJobParam());
            HtSourceInfo htSourceInfo =
                    htSourceInfoDao.selectOne(new LambdaQueryWrapper<HtSourceInfo>().eq(HtSourceInfo::getSourceId,
                            commonJobParam.getSourceId()));
            if (Objects.isNull(htSourceInfo)) {
                return;
            }

            Integer partnerId = Integer.parseInt(htSourceInfo.getSourceAppId());
            String accessKey = (String) JsonUtil.toMap(htSourceInfo.getExtra()).get("accessKey");
            String secretKey = (String) JsonUtil.toMap(htSourceInfo.getExtra()).get("secretKey");


            meituanTaskService.syncHotelStaticInfoPop(partnerId, accessKey, secretKey);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        XxlJobHelper.log(XxlJobNameConstant.SYNC_MTHDOM_HOTEL_STATIC_INFO_POP + " xxl job end");
    }
}